import input from './input'
import switchItem from './switch'
import tags from './tag'
import label from './label'
import image from './image'
import button from './button'
import buttons from './buttons'
import dropdown from './dropdown'
import timeText from './timeText'
import countdown from './countdown'
import tagstatus from './tagstatus'
import sort from './sort'

const map = {
  input,
  switch: switchItem,
  tags,
  label,
  image,
  button,
  buttons,
  dropdown,
  timeText,
  countdown,
  tagstatus,
  sort
}


function getConfig(props) {
  const result = []

  Object.keys(props).forEach(key => {


    let prop = props[key]
    // console.log(prop,'prop================================')
    // {}
    // {required:true}
    // {type: Number}
    // {type: [Number]}
    // [Number]
    // Number
    if (!prop.type) {
      const types = [Number, String, Boolean, Object, Function, Array]
      prop = {
        type: Array.isArray(prop) ? prop : (types.includes(prop) ? prop : String)
      }
    }
    prop.type = Array.isArray(prop.type) ? prop.type : [prop.type]
    if (prop.type.includes(String)) {
      result.push({
        label: key,
        prop: key,
        type: 'input'
      })
    } else if (prop.type.includes(Number)) {
      result.push({
        label: key,
        prop: key,
        type: 'number',
        props: {
          controlsPosition: 'right'
        }
      })
    } else if (prop.type.includes(Boolean)) {
      result.push({
        label: key,
        prop: key,
        type: 'switch',
        props: {}

      })
    } else if (prop.type.includes('select')) {
      result.push({
        label: key,
        prop: key,
        type: 'select',
        options: prop.options
      })
    }
    else if (prop.type.includes('radio')) {
      result.push({
        label: key,
        prop: key,
        type: 'radio',
        options: prop.options,
        props: prop.props,
      })
    }else if (key === 'value' && prop.type.includes(Array)) {
      result.push({
        label: key,
        prop: key,
        type: 'inputTag',
        props: {}
      })
    }
  })
  return result
}


function transform(map) {
  const result = {}
  Object.keys(map).forEach(key => {
    result[key] = getConfig(map[key])
  })
  return result
}

export default transform(map)
